package org.jetnuke.modules.story_archive;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Stories;

public class Archives implements IAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		Iterator it =  DataAccessDriver.getInst().newStoriesDAO().getStoriesTimeDesc().iterator();
			//storiesPeer.doSelect(
				//new Criteria().addDescendingOrderByColumn(storiesPeer.TIME))
				//.iterator();
		Calendar c = Calendar.getInstance();
		int thismonth = -1;
		int thisyear = -1;
		ArrayList ls1 = new ArrayList();
		while (it.hasNext()) {
			Date dt = ((Stories) it.next()).getTIME();
			c.clear();
			c.setTime(dt);
			int month = c.get(Calendar.MONTH);
			int year = c.get(Calendar.YEAR);
			if (month != thismonth || year != thisyear) {
				thisyear = year;
				thismonth = month;
				ls1.add(dt);
			}

		}
		vc.put("months", ls1);
		return new ActionForward(op, info);
	}

}
